package com.example.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.example.db.model.IPage;

/**
 * @Classname Results
 * @Description TODO
 * @Date 2022/5/26 15:31
 * @Created by XiongXiong
 * @Author: X.I.O
 */
public class Results {

    private static JSONObject getJSONObject(){
        JSONObject obj= null;
        obj=new JSONObject();
        obj.put("code",0);
        obj.put("msg","success");
        return obj;
    }

    public static JSONObject success(){
        return getJSONObject();
    }

    public static JSONObject success(String msg){
        JSONObject obj=getJSONObject();
        obj.put("msg",msg);
        return obj;
    }

    public static JSONObject success(String msg,Object obj){
        JSONObject data = getJSONObject();
        data.put("msg",msg);
        data.put("data",obj);
        return data;
    }

    public static JSONObject success(Object obj){
      JSONObject data = getJSONObject();
      data.put("data",obj);
        ValueFilter filter = new ValueFilter() {
            @Override
            public Object process(Object obj, String s, Object v) {
                if(v==null){
                    return "";
                }
                return v;
            }
        };
        String json= JSON.toJSONString(data, filter);
        return JSON.parseObject(json);
    }


    public static <T> JSONObject successLayuiTable(IPage<T> page){
        JSONObject obj=success();
        obj.put("count",page.getTotal());
        obj.put("data",page.getData());
        ValueFilter filter = new ValueFilter() {
            @Override
            public Object process(Object obj, String s, Object v) {
                if(v==null){
                    return "";
                }
                return v;
            }
        };
        String json= JSON.toJSONString(obj, filter);
        return JSON.parseObject(json);
    }

    public static JSONObject error(String msg){
        JSONObject data = getJSONObject();
        data.put("code",1);
        data.put("msg",msg);
        return data;
    }
}
